package com.List;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * Description:集合回顾代码
 * User: cp
 * Date: 2023-12-19
 * Time: 10:55
 */
public class TestDemo1 {
    public static void main(String[] args) {
        //创建ArrayList的对象
        ArrayList<Integer> list = new ArrayList();
        //添加元素
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        //下标遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) +" ");
        }
        System.out.println(

        );

        //删除按下标
        System.out.println(list.remove(3));

        //forEach
        for(Integer i : list){
            System.out.print(i +" ");
        }
        System.out.println();

        //删除按值
        System.out.println(list.remove(1));

        //迭代器遍历
        Iterator<Integer> it = list.iterator();
        while(it.hasNext()){
            int n = it.next();
            System.out.print(n +" ");
        }
        System.out.println();
    }
}
